package org.jeecg.modules.yw.job;

import java.util.List;

import javax.annotation.Resource;

import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.entity.Messages;
import org.jeecg.modules.yw.entity.Order;
import org.jeecg.modules.yw.service.AppointmentService;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.service.MessagesService;
import org.jeecg.modules.yw.service.OrderService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class InitStaffDealJob  implements Job{
	@Resource
	private OrderService orderService;
	
	@Resource
	private MessagesService messageService;
	
	@Resource
	private EnterPriseService enterpriseService;
	
	@Resource
	private AppointmentService appointmentService;
	
    
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();		
		queryWrapper.eq("msgType", 1);
		queryWrapper.eq("state", 0);
	    queryWrapper.eq("delFlag", 0);
	    queryWrapper.eq("orderMsgType", 1);
	    List<Messages> list = messageService.getBaseMapper().selectList(queryWrapper);	    
	    for (Messages obj : list) {	    	
	    	String currentUserId = obj.getCurrentUserId();	    	
	    	EnterPrise ent = enterpriseService.getById(currentUserId);	    	
	    	if (ent!=null && ent.getIsleader()!=null && ent.getIsleader().equals("2")) {
	    	 	Messages message = new Messages();
		    	message.setId(CommonUtils.getID());
		    	Order order = orderService.getBaseMapper().getOrder(obj.getOrderId());
		    	message.setOrderId(order.getId());
		    	message.setOrderNo(order.getOrderNo());
		    	message.setYyTime(order.getYyTime());
		    	message.setOwnerAddress(order.getOwnerAddress());
		    	message.setMsgType(2);
		    	message.setNormalMsgType(0);
		    	message.setState(0);
		    	message.setMsgTime(DateUtils.getCurrentTimeDateStr());
		    	message.setCurrentUserId(ent.getId());
		    	message.setDelFlag(0);	  
		    	message.setRemark("订单编号"+obj.getOrderNo()+"超时未接收，系统默认为拒绝");		    	
		    	String yyTime = order.getYyTime();		    	
		    	//if (yyTime.substring(0,10).equals(DateUtils.getDateStr())) {			    	
			    	messageService.save(message);
			    	messageService.getBaseMapper().confirmMessage(obj.getId(), "订单编号"+obj.getOrderNo()+",超时未接收，系统默认为拒绝");		
			    	appointmentService.getBaseMapper().updateAppointmentDeny(ent.getId(), obj.getOrderId(),DateUtils.getDateStr());		
		    	//}
	    	}
	    	
	    }	    
    }
}
